package com.Al.pattern.lazy;

/**
 * @Author lizhigang Date: 2019/4/17 22:27
 * @description:饿汉式-双重锁
 * 实例化的时候才会创建
 */

public class LazyDoubleCheckSingleton {
    private volatile static LazyDoubleCheckSingleton lazy = null;

    private LazyDoubleCheckSingleton(){}
    public static LazyDoubleCheckSingleton getInstance(){
        if(lazy == null){
            synchronized (LazyDoubleCheckSingleton.class){
                if(lazy == null){
                    //1.分配内存给这个对象
                    //2.初始化对象
                    //3.设置lazy指向刚分配的内存地址 会出现指令重排序问题 用volatile解决
                    //4.初次访问对象
                    lazy = new LazyDoubleCheckSingleton();
                }
            }
        }
        return lazy;
    }
}
